package com.example.f8399.testdemo.activity;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.f8399.testdemo.BaseActivity;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.adapter.BaseBindingAdapter;
import com.example.f8399.testdemo.adapter.BindingViewHolder;
import com.example.f8399.testdemo.databinding.ActivityAxureBinding;
import com.example.f8399.testdemo.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class RecyclerMutiLayoutActivity extends BaseActivity{

    ActivityAxureBinding mBinding;
    private ArrayList<ModelDataEntity> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_axure);
        mBinding.setAc(this);

        initView();


    }

    private void initView() {
        initData();

        LinearLayoutManager layout = new LinearLayoutManager(this);
        mBinding.rlvTotal.setLayoutManager(layout);
        DividerItemDecoration decor = new DividerItemDecoration(this, layout.getOrientation());
        mBinding.rlvTotal.addItemDecoration(decor);
        final RcMutiAdapter adapter = new RcMutiAdapter(this);
        mBinding.rlvTotal.setAdapter(adapter);
        adapter.setData(mList);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ModelDataEntity modelDataEntity = adapter.getData().get(position);
                ToastUtils.showShort(modelDataEntity.getClass().getSimpleName());
            }
        });
    }

    private void initData() {
        ArrayList<RvEntity1> list1 = new ArrayList<>();
        ArrayList<RvEntity2> list2 = new ArrayList<>();
        ArrayList<RvEntity3> list3 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            RvEntity1 entity1 = new RvEntity1("name-RvEntity1"+i,i*10,String.valueOf(i*30+33));
            list1.add(entity1);
        }

        for (int i = 0; i < 6; i++) {
            RvEntity2 rvEntity2 = new RvEntity2("name-RvEntity2"+i,i*10,String.valueOf(i*30+33));
            list2.add(rvEntity2);
        }

        for (int i = 0; i < 2; i++) {
            RvEntity3 rvEntity3 = new RvEntity3("name-RvEntity3"+i,i*10,String.valueOf(i*30+33));
            list3.add(rvEntity3);
        }

        mList = new ArrayList<>();
        mList.addAll(list1);
        mList.addAll(list2);
        mList.addAll(list3);


    }

    private class RvEntity1 implements ModelDataEntity{
        public int type =1;
        private String name;
        private int age;
        private String credit;

        public RvEntity1(String name, int age, String credit) {
            this.name = name;
            this.age = age;
            this.credit = credit;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getCredit() {
            return credit;
        }

        public void setCredit(String credit) {
            this.credit = credit;
        }
    }

    private class RvEntity2 implements ModelDataEntity{
        public int type =2;

        private String name;
        private int age;
        private String credit;

        public RvEntity2(String name, int age, String credit) {
            this.name = name;
            this.age = age;
            this.credit = credit;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getCredit() {
            return credit;
        }

        public void setCredit(String credit) {
            this.credit = credit;
        }
    }

    private class RvEntity3 implements ModelDataEntity{
        public int type =3;
        private String name;
        private int age;
        private String credit;

        public RvEntity3(String name, int age, String credit) {
            this.name = name;
            this.age = age;
            this.credit = credit;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getCredit() {
            return credit;
        }

        public void setCredit(String credit) {
            this.credit = credit;
        }
    }

    private interface ModelDataEntity {
    }

    private class RcMutiAdapter extends BaseBindingAdapter<ModelDataEntity>{

        private Context mContext;
        public RcMutiAdapter(Context context) {
            super(context);
            this.mContext = context;
        }

        @Override
        protected int getLayoutId(int viewType) {
            switch (viewType) {
                case 10:
                    return R.layout.item_rv_muti_1;
                case 11:
                    return R.layout.item_rv_muti_2;
                case 12:
                    return R.layout.item_rv_muti_3;
            }
            return R.layout.activity_empty_rc;
        }


        @Override
        public int getItemViewType(int position) {
            ModelDataEntity modelDataEntity = getData().get(position);
            if (modelDataEntity instanceof RvEntity1) {
                return 10;
            }
            if (modelDataEntity instanceof RvEntity2) {
                return 11;
            }
            if (modelDataEntity instanceof RvEntity3) {
                return 12;
            }
            return super.getItemViewType(position);
        }

        @Override
        protected void onBindItem(@NonNull final BindingViewHolder holder, int position) {
            ViewDataBinding binding = holder.getBinding();
            View root = binding.getRoot();
            ModelDataEntity modelDataEntity = getData().get(holder.getAdapterPosition());
            if (modelDataEntity instanceof RvEntity1) {

                RvEntity1 entity1 = (RvEntity1) modelDataEntity;
                TextView tv_name = root.findViewById(R.id.tv_name);
                tv_name.setText(entity1.getName());
            } else if (modelDataEntity instanceof RvEntity2) {
                RvEntity2 entity2 = (RvEntity2) modelDataEntity;
                TextView tv_name = root.findViewById(R.id.tv_name);
                tv_name.setText(entity2.getName());
            } else if (modelDataEntity instanceof RvEntity3) {
                RvEntity3 entity3 = (RvEntity3) modelDataEntity;
                //gridview
                RecyclerView rlv_gridview = root.findViewById(R.id.rlv_gridview);
                GridLayoutManager layout = new GridLayoutManager(mContext, 3);
                rlv_gridview.setLayoutManager(layout);
                final ChildAdapter childAdapter = new ChildAdapter(mContext);
                rlv_gridview.setAdapter(childAdapter);
                //initdata
                ArrayList<String> list_msg = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    list_msg.add("msgg" + i);
                }
                childAdapter.setData(list_msg);
                childAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        ToastUtils.showShort(childAdapter.getData().get(position));
                    }
                });
            }

            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(holder.getLayoutPosition());
                    }
                }
            });

        }
    }

    private class ChildAdapter extends BaseBindingAdapter<String>{
        Context mContext;
        public ChildAdapter(Context context) {
            super(context);
            this.mContext = context;
        }

        @Override
        protected int getLayoutId(int viewType) {
            return R.layout.item_gv_child1;
        }

        @Override
        protected void onBindItem(@NonNull final BindingViewHolder holder, int position) {
            ViewDataBinding binding = holder.getBinding();
            View root = binding.getRoot();
            TextView tv_msg = root.findViewById(R.id.tv_msg);
            tv_msg.setText(getData().get(holder.getAdapterPosition()));

            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(holder.getLayoutPosition());
                    }
                }
            });
        }
    }
}
